package org.example.ui;

import org.example.dao.DeptDao;
import org.example.dao.impl.DeptDaoImpl;
import org.example.entity.Dept;
import org.example.entity.EmployeeDTO;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class DepartmentManagementPanel extends JPanel {
    JTextField searchField;
    JButton searchButton;
    JTable deptTable;
    DeptDao deptDao=new DeptDaoImpl();
    public DepartmentManagementPanel() {
        setBackground(new Color(40, 40, 40));
        setLayout(new BorderLayout());
        JPanel topPanel = new JPanel();
        topPanel.setBackground(new Color(40, 40, 40));
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        JLabel nameLabel = new JLabel("部门名称:");
        nameLabel.setForeground(Color.WHITE);
        topPanel.add(nameLabel);

        searchField = new JTextField(15);
        topPanel.add(searchField);

        searchButton = new JButton("搜索");
        topPanel.add(searchButton);

        add(topPanel, BorderLayout.NORTH);
        String[] columnNames = {"部门ID","部门名称","部门地址"};
        DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
        deptTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(deptTable);

        add(scrollPane, BorderLayout.CENTER);
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String deptNameToSearch = searchField.getText();
                Dept deptDetail = findDeptByName(deptNameToSearch); // 假设存在此方法来根据名字查找员工
                if (deptDetail != null) {
                    showEmployeeDetailsDialog(deptDetail);
                } else {
                    JOptionPane.showMessageDialog(DepartmentManagementPanel.this,
                            "未找到匹配的员工: " + deptNameToSearch, "搜索结果",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            }
            private Dept findDeptByName(String name) {
                // 这里应该有一个实际的数据获取逻辑，例如查询数据库或调用服务。
                // 示例返回值，应替换为实际数据获取逻辑。
                return deptDao.getDeptByName(name); // 或者返回匹配的EmployeeDTO对象
            }
            private void showEmployeeDetailsDialog(Dept dept) {
                JDialog dialog = new JDialog((JFrame)null, "部门详情", true);
                dialog.setSize(300, 200);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setLayout(new GridLayout(7, 2));

                dialog.add(new JLabel("部门ID:"));
                dialog.add(new JLabel(String.valueOf(dept.getDeptId())));
                dialog.add(new JLabel("部门名称:"));
                dialog.add(new JLabel(dept.getDeptName()));
                dialog.add(new JLabel("部门地址:"));
                dialog.add(new JLabel(dept.getDeptLoc()));

                dialog.pack();
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }
        });
        // 初始化表格数据
        initializeTableData();
        centerAlignTableColumns();

    }
    private void initializeTableData() {
        // 清除现有数据
        ((DefaultTableModel) deptTable.getModel()).setRowCount(0);

        // 假设有一个方法可以获取所有的员工列表
        List<Dept> depts = loadAllEmployees(); // 应替换为实际的数据加载逻辑

        // 将数据填充到表格
        for (Dept dept: depts) {
            ((DefaultTableModel)deptTable.getModel()).addRow(new Object[]{
                   dept.getDeptId(),
                   dept.getDeptName(),
                   dept.getDeptLoc()

            });
        }
    }

    private List<Dept> loadAllEmployees() {
        // 此处应有从数据库或其他数据源加载员工数据的逻辑
        // 返回一个模拟的员工列表作为示例
        List<Dept> employeDTOs = deptDao.getAllDepts();
        return employeDTOs; // 应替换为实际的员工数据列表
    }

    // 在 EmployeeManagementPanel 类中添加以下方法
    private void centerAlignTableColumns() {
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);

        for (int i = 0; i < deptTable.getColumnCount(); i++) {
            deptTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
        }
    }
}